<?php

require_once dirname(__FILE__).'/../bootstrap.php';
require_once dirname(__FILE__).'/../../lib/urCronServiceBase.class.php';

$t = new lime_test(null, new lime_output_color());

// variables
$testFeedUri = 'http://future.isgonna.be/stream/back.xml';
$testFeedText = '<xml></xml>';
$testFeedId = 0;
$t->diag('urCronServiceBase');
$serviceBase = new urCronServiceBase();

// ->setFeedId();
$t->diag('::setFeedId()');
$serviceBase->setFeedId($testFeedId);
$t->is($serviceBase->getFeedId(), 0, '::setFeedId() & ::getFeedId() are ok');

// ->setFeedUri()
$t->diag('::setFeedUri()');
$serviceBase->setFeedUri($testFeedUri);
$t->is($serviceBase->getFeedUri(), $testFeedUri, '::setFeedUri() & ::getFeedUri() are ok');

// ->doCronJob()
$t->diag('::doCronJob()');
$serviceBase = new urCronServiceBase();
$serviceBase->setFeedId($testFeedId);
try
{
  $serviceBase->doCronJob();
  $t->fail('::doCronJob() throw sfException if no "feedUri" is specified');
}
catch(sfException $e)
{
  $t->pass('::doCronJob() throw sfException if no "feedUri" is specified');
}

$serviceBase = new urCronServiceBase();
$serviceBase->setFeedUri($testFeedUri);
try
{
  $serviceBase->doCronJob();
  $t->fail('::doCronJob() throw sfException if no "feedId" is specified');
}
catch(sfException $e)
{
  $t->pass('::doCronJob() throw sfException if no "feedId" is specified');
}

// Can't access the feed or feed is incorrect
$serviceBase = new urCronServiceBase();
$serviceBase->setFeedUri($testFeedUri);
$serviceBase->setFeedId(23);
try
{
  @$serviceBase->doCronJob();
  $t->fail("::doCronJob() throw Exception if it can\'t access the feed");
}
catch(Exception $e)
{
  $t->pass('::doCronJob() throw Exception if it can\'t access the feed');
}

$t->diag('->parseTags()');

$tagsXml = new SimpleXMLElement(<<<EOF
<entry>
	<category term="hk" scheme="http://www.flickr.com/photos/tags/" />
	<category term="hongkong" scheme="http://www.flickr.com/photos/tags/" />
	<category term="typhoonshelter" scheme="http://www.flickr.com/photos/tags/" />
</entry>
EOF
  );

$tags = array('hk', 'hongkong', 'typhoonshelter');
$t->is_deeply($serviceBase->parseTags($tagsXml), $tags, "->parseTags() return an array of tags from a SimpleXML element");
